package com.ezjie.ielts.module_read;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ProgressBar;

import com.ezjie.ielts.R;
import com.ezjie.ielts.constant.Constants;
import com.ezjie.ielts.util.UmengPages;
import com.ezjie.ielts.view.photoview.PhotoView;
import com.ezjie.ielts.view.photoview.PhotoViewAttacher.OnViewTapListener;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.umeng.analytics.MobclickAgent;

@ContentView(R.layout.activity_photoview)
public class PhotoViewActivity extends Activity {

	private static final String TAG = PhotoViewActivity.class.getSimpleName();

	@ViewInject(R.id.iv_photo)
	private PhotoView iv_photo;
	@ViewInject(R.id.pb_loading)
	private ProgressBar pb_loading;

	private static DisplayImageOptions options;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ViewUtils.inject(this);

		String url = getIntent().getStringExtra(Constants.READ_IMG_URL);

		options = new DisplayImageOptions.Builder().showStubImage(R.drawable.default_img) // 设置图片在下载期间显示的图片
				.showImageForEmptyUri(R.drawable.default_img) // 设置图片Uri为空或是错误的时候显示的图片
				.showImageOnFail(R.drawable.default_img) // 设置图片加载/解码过程中错误时候显示的图片
				.cacheInMemory() // 是否緩存都內存中
				.cacheOnDisc() // 是否緩存到sd卡上
				.build();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
		ImageLoader.getInstance().init(config);

		ImageLoader.getInstance().displayImage(url, iv_photo, options);

		iv_photo.setOnViewTapListener(new OnViewTapListener() {

			@Override
			public void onViewTap(View view, float x, float y) {
				finish();
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(UmengPages.PAGE_PHOTOVIEW);
		MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(UmengPages.PAGE_PHOTOVIEW);
		MobclickAgent.onPause(this);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (event.getAction() == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

}
